// pages/parentKetangjilu/parentKetangjilu.js
var dateUtils = require('../../utils/dateUtil.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseTime1:'05-01',
    chooseTime2:'05-03',
    chooseTimes1:0,
    chooseTimes2:0,
    array: ['本人记录', '他人记录'],
    index: 0,
    userInfo: {},
    teacherName:"",
    headImg:"",
    dataList:[],
    totalPage:0,
    pageNo:1,
    pageSize:10,
    isLoading:false,
    flag:0,  //0本人1 非本人
    ifShowUI:false, //默认有数据

    //班级选择----------begin
    bottom:false,
    //校区
    schoolArealist:[],
    schoolArea:'',
    schoolAreaKey:'',
    //班级类型
    classTypes: [],
    classTypesKey:'',
    //学段
    // seasons:[],
    // seasonKey:'',
    // seasonName:'',
    //年级
    grades:[],
    gradeKey:"",
    gradeName:'',
    //班级
    classs:[],
    classKey:'',
    className:'',
    viewName:'全部班级',
    isShowDetail:true,
  },

  didClickActionShow1(){
    var action =  this.selectComponent("#action-sheet1");
    action.onShowSingelAlert(this.data.schoolArealist,1);
   },

   didClickActionShow2(){
    var action =  this.selectComponent("#action-sheet2");
    action.onShowSingelAlert(this.data.grades,1);
   },

   didClickActionShow3(){
    var action =  this.selectComponent("#action-sheet3");
    action.onShowSingelAlert(this.data.classs,1);
   },

  didClickUp(){
    this.setData({
      isShowDetail:false
    })
  },
  actionSheetIndex1(e){
    console.log(this.data.schoolArea+'==选择的数据='+e.detail.name)
    if(this.data.schoolArea != e.detail.name){
      this.setData({
        schoolArea:e.detail.name,
        schoolAreaKey:e.detail.key
      })
      this.getClassType(1)
    }
  },
  radioChange(res){
    console.log('选择班级的数据='+JSON.stringify(res))

    this.setData({
      classTypesKey:res.detail.value
    })
    this.getClassType(3)
  },
  actionSheetIndex2(e){

    console.log(e.detail.key+'选择的数据='+e.detail.name)
    this.setData({
      gradeName:e.detail.name,
      gradeKey:e.detail.key
    })
    this.getClassType(4)
  },
  actionSheetIndex3(e){
    console.log('选择的数据='+e.detail.key)
    console.log('选择的数据='+e.detail.name)
    this.setData({
      className:e.detail.name.indexOf('班')!=-1?e.detail.name:e.detail.name+"班",
      classKey:e.detail.key
    })
  },

  //获取校区
  getSchoolArea(){
    app.api.getSchoolArea().then(rest => {
      if(rest&&rest.success){   
        if(rest.data.list!=null && rest.data.list.length > 0){
           this.setData({
            schoolArealist:rest.data.list,
            schoolArea:rest.data.list[0].name,
            schoolAreaKey:rest.data.list[0].key
           })
           this.getClassType(1)
        }
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },


  //获取班级类型
  getClassType(resultType){
    var that = this
    var datas
    if(resultType == 1){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
      }
    }else if(resultType == 2){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
      }
    }else if(resultType == 3){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
        //section:this.data.seasonKey,
      }
    }else if(resultType == 4){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
        //section:this.data.seasonKey,
        grade:this.data.gradeKey,
      }
    }
    console.log('接口参数='+JSON.stringify(datas))
    app.api.getClassInfos(datas).then(rest => {
      if(rest&&rest.success){   
        if(rest.data.list!=null && rest.data.list.length > 0){    
          
          if(resultType == 1){
            this.setData({
              classTypes:rest.data.list,
             })
             this.data.classTypes[0].checked = 'true'
             this.setData({
              classTypes:rest.data.list,
              classTypesKey:rest.data.list[0].key
             })
             that.getClassType(3)
          }
          // else if(resultType == 2){
          //    this.setData({
          //     seasons:rest.data.list,
          //     seasonKey:rest.data.list[0].key,
          //     seasonName:rest.data.list[0].name,
          //    })
          //    that.getClassType(3)
          // }
          else if(resultType == 3){
            this.setData({
              grades:rest.data.list,
              gradeKey:rest.data.list[0].key,
              gradeName:rest.data.list[0].name,
            })
            that.getClassType(4)
          }else if(resultType == 4){
            this.setData({
              classs:rest.data.list,
              classKey:rest.data.list[0].key,
              className:rest.data.list[0].name.indexOf('班')!=-1?rest.data.list[0].name:rest.data.list[0].name+"班",
            })
          }
        }else{
          //没有数据复位
          if(resultType == 1){
             this.setData({
              classTypes:[],
              classTypesKey:'',
              // seasons:[],
              // seasonKey:'',
              // seasonName:'',
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
             })
          }else if(resultType == 2){
             this.setData({
              // seasons:[],
              // seasonKey:'',
              // seasonName:'',
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
             })
            
          }else if(resultType == 3){
            this.setData({
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
            })
           
          }else if(resultType == 4){
            this.setData({
              classs:[],
              classKey:'',
              className:'',
            })
          }
        }
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  bottomPop() {
  	this.setData({
  		bottom: true
    })
    this.getSchoolArea()

  },
  cancelPopup(){
    this.hidePop()
  },

  okPopup(){
    this.hidePop()

    if(this.data.className!=''){
      this.setData({
        pageNo:1,
        dataList:[],
        totalPage:0,
        viewName:this.data.className
      })
      this.getListData()
    }
  },


  allClass(){
    this.hidePop()
    this.setData({
      pageNo:1,
      dataList:[],
      totalPage:0,
      viewName:'全部班级',
      classKey:''
    })
    this.getListData()
  },

  hidePop () {
    // 关闭select  .对应class #对应id
    this.selectComponent('#popup').hidePop()
  },

  change (e) {
    // var that = this
    // var vIndex = e.detail.index
    // var stuName = e.detail.name + "同学"
    // this.setData({
    //   //name:{...e.detail.name},
    //   name:stuName,
    //   stuIndex:vIndex,
    //   //stuInfo:that.data.userInfo.list[vIndex],
    //   className:that.data.userInfo.list[vIndex].schoolName+that.data.userInfo.list[vIndex].className
    // })
     console.log(e.detail);
    // wx.setStorageSync('stuIndex', this.data.stuIndex)
  },




  bindPickerChange: function(e) {
    console.log('picker发送选择改变时间1，携带值为', e.detail.value)
    this.setData({
      flag: e.detail.value,
      pageNo:1,
      dataList:[],
      totalPage:0
      
    })
    this.getListData()
  },

  bindDateChange1: function(e) {

    let repTime = e.detail.value.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    console.log(timeTamp+' picker发送选择改变时间11，携带值为', e.detail.value)
    if(timeTamp <= this.data.chooseTimes2){
      this.setData({ 
        chooseTimes1:timeTamp,
        chooseTime1: e.detail.value.substring(5, 10),
        pageNo:1,
        dataList:[],
        totalPage:0
      })
      this.getListData()
    }else{
      wx.showToast({
        title: '开始时间需小于结束时间',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }
   
    
  },
  bindDateChange2: function(e) {
    
    let repTime = e.detail.value.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    console.log(timeTamp+"==="+this.data.chooseTimes1+' picker发送选择改变时间22，携带值为', e.detail.value)

    if(timeTamp>=this.data.chooseTimes1){
      this.setData({
        chooseTimes2:timeTamp,
        chooseTime2: e.detail.value.substring(5, 10),
        pageNo:1,
        dataList:[],
        totalPage:0
      })
      this.getListData()
    }else{
      wx.showToast({
        title: '结束时间需大于开始时间',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    }

   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    var userInfos = wx.getStorageSync('userData');//app.userInfo;
     if(userInfos !=null){
       var tHeadImge
       
       if(userInfos.headUrl !=null){
          tHeadImge = userInfos.headUrl
        }else{
          // if(userInfos.list[0].sex == 1){
          //   tHeadImge = "/images/tx.png"
          // }else{
            tHeadImge = "/images/txn.png"
          //}
       }
      this.setData({
        teacherName:userInfos.realName!=null?userInfos.realName:'',
        headImg:tHeadImge
      })
     }
     console.log('ssssssssss'+JSON.stringify(app.userInfo))
     
     
      var starTiem = dateUtils.mathChangeDate(dateUtils.formatTime(new Date()),"-",7,true,'month-date')
      var endTiem = dateUtils.formatTimeMD(new Date())

     //时间日期格式
      var endformtTiem = new Date(dateUtils.formatDataTime(new Date())).getTime()
      var starformtTiem = new Date(dateUtils.mathChangeDate(dateUtils.formatDataTime(new Date()),"-",7,false)).getTime()

      this.setData({
        chooseTime1:starTiem,
        chooseTime2:endTiem,
        chooseTimes1:starformtTiem,
        chooseTimes2:endformtTiem,
     })
   this.getListData()
  },

  getListData(cd){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading:true
    })
    var data
     if(this.data.classKey!=null && this.data.classKey!=''){
        data = {
          beginTime:this.data.chooseTimes1,
          endTime:this.data.chooseTimes2,//"1653177295000"
          pageNo:this.data.pageNo,
          pageSize:this.data.pageSize,
          flag:this.data.flag,
          classId:this.data.classKey
        }
     }else{
        data = {
          beginTime:this.data.chooseTimes1,
          endTime:this.data.chooseTimes2,//"1653177295000"
          pageNo:this.data.pageNo,
          pageSize:this.data.pageSize,
          flag:this.data.flag,
        }
     }
    
    console.log('接口参数时间='+JSON.stringify(data))
    app.api.getTeacherCourset(data).then(rest => {
      wx.hideLoading()
      this.setData({
        isLoading:false
      })
      cd && cd()
      if(rest&&rest.success){   
        console.log('接口数据='+JSON.stringify(rest))
        if(rest.data.list!=null && rest.data.list.length > 0){
           this.setData({
            ifShowUI:false
           })
        }else{
          this.setData({
            ifShowUI:true
           })
        }
        this.setData({
          dataList:[...this.data.dataList,...rest.data.list],
          totalPage:rest.data.totalCount
        })
       
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.setData({
    //   pageNo:1,
    //   dataList:[],
    //   totalPage:0
    // })
    // this.getListData(()=>{
    //   wx.stopPullDownRefresh()
    // })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // console.log('触地时间')
    // if(this.data.pageSize * this.data.pageNo >= this.data.totalPage){
    //   return
    // }

    // if(this.data.isLoading){
    //   return
    // }

    // this.setData({
    //   pageNo:this.data.pageNo + 1
    // })
    // this.getListData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 加载更多
  loadmore({
    detail
  }) {
    console.log('加载更多11='+this.data.pageSize * this.data.pageNo)
    if(this.data.pageSize * this.data.pageNo >= this.data.totalPage){
      wx.showToast({
        title: '暂无更多记录！',
        icon: 'none',
      })
      this.setData({
        isLoading:false
      })

      detail.success();

      return
    }

    if(this.data.isLoading){
      return
    }

    this.setData({
      pageNo:this.data.pageNo + 1
    })
    this.getListData(()=>{
      detail.success();
    })
    
  },
  // 刷新
  refresh({
    detail
  }) {
    console.log('下拉刷新='+this.data.pageSize * this.data.pageNo)
    this.setData({
      pageNo:1,
      dataList:[],
      totalPage:0
    })
    this.getListData(()=>{
      detail.success();
    })
  },
})